checkLogin(true);
$(function () {
    $.post("../php/categories/getCategoriesAll.php", function (res) {
        if (res.code == 100) {
            var html = "<option value=''>所有分类</option>";
            for (var i = 0; i < res.data.length; i++) {
                html += "<option value='" + res.data[i].id + "'>" + res.data[i].name + "</option>";
            }
            $("#category").html(html);
        }
    }, "json");

    $("#feature").on("change", function () {
        var file = this.files[0];
        var data = new FormData();
        data.append("file", file);
        var xhr = new XMLHttpRequest();
        xhr.open("post", "../php/common/uploadFile.php");
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                var res = JSON.parse(xhr.responseText);
                if (res.code == 100) {
                    $("#img-file").attr("src", res.path).show();
                    $("#featuredata").val(res.path);
                }
                else {
                    layer.alert(res.msg, {icon: 2});
                }
            }
        }
        xhr.send(data);
    });

    $("#btn-sure").on("click", function () {
        var data = $("#articledata").serialize();
        if (location.search) {
            var id = article_id;
            data += "&ID=" + id;
            $.post("../php/articles/updateArticleById.php", data, function (res) {
                if (res.code == 100) {
                    layer.confirm("您是想查看结果还是继续编辑呢？点击确定查看，点击取消继续编辑。", {icon: 1}, function (index) {
                       if(index){
                           location.href = "articles.html";
                       }
                    })
                }
            }, "json");
        }
        else {
            var user_id = $.cookie("user_id");
            data += "&user_id=" + user_id;
            $.post("../php/articles/addArticle.php", data, function (res) {
                if (res.code == 100) {
                    layer.confirm("您是想查看结果还是继续编辑呢？点击确定查看，点击取消继续编辑。", {icon: 1}, function (index) {
                        if(index){
                            location.href = "articles.html";
                        }
                    })
                }
            }, "json");
        }
        return false;
    })

    if (location.search) {
        var article_id = location.search.split("=")[1];
        $.post("../php/articles/getArticlById.php", {id: article_id}, function (res) {
            if (res.code == 100) {
                $("#title").val(res.data.title);
                $("#content").val(res.data.content);
                $("#slug").val(res.data.slug);
                $("#img-file").attr("src", res.data.feature).show();
                $("#featuredata").val(res.data.feature);
                $("#category").val(res.data.category_id);
                $("#created").val(res.data.created.replace(" ", "T"));
                $("#status").val(res.data.status);
            }
        }, "json");
    }
});